<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\index\logic;

use app\event\ModifyIntegral;
use app\model\IntegralLog;
use app\model\SignIn;
use app\model\User;
use app\model\UserSign;
use think\facade\Db;
use think\facade\Request;

class SignLogic
{
    /*
     * 根据不同的职位 有不同的参数
     * 保洁 1 单纯的打卡
     * 领班 2 上传图片或者统计签到人数
     */
    public function signIn(){

        $orgId = config('user')['orgid'];
        $uid = config('user')['id'];
        $position = config('user')['position'];
        $signModel = new SignIn();
        //默认 保洁
        $sign = [
            'sign_uid' => $uid,
            'sign_orgid' => $orgId,
            'sign_time' => time(),
            'sign_position' => User::CLEANING,
        ];

        $userSignData = [
            'us_uid' => $uid,
            'us_orgid' => $orgId,
            'us_create_time' => time(),
            'us_position' => User::CLEANING,
        ];
        if(checkPosition($position,User::CLEANING_HEAD) && config('user')['isphoto'] == 1){
            //领班
            $param = input('post.');
            if(isset($param['pic']) && !empty($param['pic'])){
                $sign['sign_pic'] = fileFullPath(trim($param['pic']));
                $userSignData['us_pic'] = fileFullPath(trim($param['pic']));
            }

            if(!isset($param['must']) || empty($param['must'])) fail('请填写出勤人数');
            $sign['sign_must'] = intval($param['must']);
            $userSignData['us_must'] = intval($param['must']);
            if(!isset($param['real']) || empty($param['real'])) fail('请填写实到人数');
            $sign['sign_real'] = intval($param['real']);
            $userSignData['us_real'] = intval($param['real']);
//            if(!isset($param['type']) || empty($param['type'])) fail('请选择考勤类型');
//            $sign['sign_type'] = intval($param['type']);
            $sign['sign_position'] = User::CLEANING_HEAD;
            $userSignData['us_position'] = User::CLEANING_HEAD;
        }

        //判断当天是否打过卡
        $where = [
            ['us_start_time','>=',strtotime(date('Y-m-d'))],
            ['us_start_time','<=',strtotime(date('Y-m-d 23:59:59'))],
            ['us_create_time','<=',strtotime(date('Y-m-d 23:59:59'))],
            ['us_create_time','>=',strtotime(date('Y-m-d'))],
            ['us_uid','=',$uid],
            ['us_status','=',1],
        ];
        $userSignModel = new UserSign();
        $userSignInfo = $userSignModel->getSignInfoByWhere($where);
        trace($userSignInfo);
        if(!empty($userSignInfo)) $sign['sign_type'] = 2;

        $sendIntegralFlag = true;
        if(!empty($userSignInfo['us_end_time'])) $sendIntegralFlag = false;

        Db::startTrans();
        if($userSignInfo){
            $update = [
                'us_end_time' => time()
            ];
            $userSignModel->updateSignInfoById($userSignInfo['us_id'],$update);
        }else{
            $userSignData['us_start_time'] = time();
            $userSignModel->addOneSign($userSignData);
        }


        $rt = $signModel->addOneSign($sign);
        if(!$rt){
            Db::rollback();
            fail('签到打卡失败');
        }

        $num = 0;
        if($sendIntegralFlag){
            //赠送积分
            $num = event(new ModifyIntegral(IntegralLog::SIGN_IN,$rt,'打卡'));
            $num = is_array($num) ? $num[0] : $num;
        }

        Db::commit();
        return $num;

    }

    public function getSignNum(){
        $uid = config('user')['id'];

        $signModel = new SignIn();
        $num = $signModel->getSignInTotalNumByUid($uid);
        return $num ? $num : 0;
    }
}